package stone.eval;

import stone.Name;
import stone.NumberLiteral;
import stone.StoneException;
import stone.Token;
import stone.parser.ASTLeaf;
import stone.parser.ASTList;
import stone.parser.ASTree;
import stone.parser.StringLiteral;

import java.util.List;

public class BasicEvaluator {
    public static final int TRUE = 1;
    public static final int FALSE = 0;

    public static abstract class ASTreeEx extends ASTree{
        public abstract Object eval(Environment env);
    }

    public static class ASTListEx extends ASTList{

        public ASTListEx(List<ASTree> children) {
            super(children);
        }

        public Object eval(Environment env){
            throw new StoneException("cannot eval:"+toString());
        }
    }

    public static class ASTLeafEx extends ASTLeaf{

        public ASTLeafEx(Token token) {
            super(token);
        }

        public Object eval(Environment env){
            throw new StoneException("cannot eval:"+toString());
        }
    }

    public static class NumberEx extends NumberLiteral{

        public NumberEx(Token token) {
            super(token);
        }

        public Object eval(Environment e){
            return value();
        }
    }

    public static class StringEx extends StringLiteral{

        public StringEx(Token t) {
            super(t);
        }

        public Object eval(Environment e){
            return value();
        }
    }

    public static class NameEx extends Name {

        public NameEx(Token token) {
            super(token);
        }

        public Object eval(Environment env){
            Object value=env.get(name());
            if (value==null){
                throw new StoneException("undefind name "+ name());
            }else {
                return value;
            }
        }
    }

}
